#!/bin/bash

# Copyright (c) 2011 Narendra Sisodiya http://narendrasisodiya.com
# Email - narendra@narendrasisodiya.com
# Released under GPLv3
# http://code.google.com/p/debbundle

#	This file is par of debbundle - http://code.google.com/p/debbundle
#	debbundle is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	debbundle is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with debbundle.  If not, see <http://www.gnu.org/licenses/>.




echo "  =======   Welcome to OneClickInstall Deb Creator	  ========"

FULLFILENAME=$1

if [ -z "$FULLFILENAME" ]
then

cat << EOF
usage: $0 OutPutFile package-list
Ex - $0 vim vim-gtk

EOF
  exit 1;
fi


RES=`which makeself > /dev/null;echo $?`

if [ $RES = 1 ]
then
	echo "Makeself command was not installed, Please install makeself command"
	echo "Run following to install makeself"
	echo "sudo apt-get install makeself"
	exit 0
fi

shift
ALLPACKAGES=$@

echo "Creating a One Click Installer to install $FULLFILENAME ....................."
echo "......."
echo "............."
echo "....................."
## moving old archives to new directory  

LocalDir=./$FULLFILENAME

mkdir -p $LocalDir
mkdir -p $LocalDir/partial

sudo apt-get -y -o dir::cache::archives="$LocalDir"  install --download-only $ALLPACKAGES
sudo rm $LocalDir/lock
touch $LocalDir/install.sh
chmod +x $LocalDir/install.sh

### Fixing QuickFix 
### Computer which do have have internet cannot run 'apt-get update' and hence apt-get install
###

cat << EOF > $LocalDir/install.sh
#!/bin/bash
#sudo apt-get -o dir::cache::archives=\$PWD install $ALLPACKAGES
echo "Installation started... We are going to install following packages..............."
echo "$ALLPACKAGES"
sudo dpkg -i ./*.deb
echo "......................................!!!!!!!!!!!!..............."
echo "Press Enter to exit installation ..............."
read AA
exit 0
EOF


### makeself this into a bundle

makeself $PWD/$FULLFILENAME $FULLFILENAME.`arch`.OneClickInstall "Installing $FULLFILENAME ..........." ./install.sh

exit 0
